<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('项目标签配置') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('配置名称')" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="$t('配置名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('配置编码')" prop="encode">
        <el-input
          v-model="queryParams.encode"
          :placeholder="$t('配置编码')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:tags:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dealTagsConfigureList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('配置名称')" align="center" prop="name" show-overflow-tooltip />
      <el-table-column :label="$t('配置编码')" align="center" prop="encode" show-overflow-tooltip />
      <el-table-column :label="$t('标签类型')" align="center" prop="type" :formatter="typeFormat" show-overflow-tooltip />
      <el-table-column :label="$t('排序')" align="center" prop="sort" width="100" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="280">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:tags:edit']"
            size="small"
            type="text"
            icon="el-icon-receiving"
            @click="tagsManagement(scope.row)"
          >{{ $t('标签管理') }}
          </el-button>
          <el-button
            v-hasPermi="['deal:tags:edit']"
            size="small"
            type="text"
            :icon="scope.row.status === 'start' ? 'el-icon-open' : 'el-icon-turn-off'"
            @click="updateStatus(scope.row)"
          >{{ scope.row.status === 'start' ? $t('停用') : $t('启用') }}
          </el-button>
          <el-button
            v-hasPermi="['deal:tags:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}
          </el-button>
          <el-button
            v-hasPermi="['deal:tags:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目标签配置对话框 -->
    <el-dialog
      :title="$t(title)"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
    >
      <dealTagsConfigureForm ref="dealTagsConfigureForm" />
    </el-dialog>

    <el-dialog
      :title="$t('标签管理')"
      :visible.sync="tagsOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      width="80%"
      append-to-body
    >
      <deal-tags-info-list ref="dealTagsInfoList" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listDealTagsConfigure,
  delDealTagsConfigure,
  exportDealTagsConfigure,
  updateStatus
} from '@/api/deal/tags/dealTagsConfigure'
import dealTagsConfigureForm from './dealTagsConfigureForm.vue'
import DealTagsInfoList from '@/views/deal/tags/dealTagsInfoList'

export default {
  components: {
    DealTagsInfoList,
    dealTagsConfigureForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 项目标签配置表格数据
      dealTagsConfigureList: [],
      // 弹出层标题
      title: '项目标签配置',
      // 是否显示弹出层
      formOpen: false,
      // create新建/start启用/stop停用字典
      statusOptions: [],
      typeOptions: [],

      // 查询参数
      queryParams: {
        name: undefined,
        encode: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      tagsOpen: false
    }
  },
  created() {
    this.getList()
    this.getDicts('deal_tag_status').then(response => {
      this.statusOptions = response.data
    })
    this.getDicts('deal_type').then(response => {
      this.typeOptions = response.data
    })
  },
  methods: {
    /** 查询项目标签配置列表 */
    getList() {
      this.loading = true
      listDealTagsConfigure(this.queryParams).then(response => {
        this.dealTagsConfigureList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // create新建/start启用/stop停用字典翻译
    statusFormat(row) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    typeFormat(row) {
      if (row.type) {
        const result = row.type.map(e => {
          return this.selectDictLabel(this.typeOptions, e)
        })
        return result.join(',')
      }
      return ''
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,

        name: undefined,

        encode: undefined,

        type: undefined,

        sort: undefined,

        status: undefined,

        delFlag: undefined,

        createBy: undefined,

        createTime: undefined,

        updateBy: undefined,

        updateTime: undefined

      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.dealTagsConfigureForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.dealTagsConfigureForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delDealTagsConfigure(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return exportDealTagsConfigure(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    },
    updateStatus(row) {
      const status = row.status
      row.status = row.status === 'start' ? 'stop' : 'start'
      updateStatus(row.id, row.status).then(res => {
        if (res.code === 200) {
          this.$message({
            message: this.$t('修改状态成功'),
            type: 'success'
          })
        } else {
          row.status = status
          this.$message({
            message: this.$t('修改状态失败'),
            type: 'error'
          })
        }
      })
    },
    tagsManagement(row) {
      this.tagsOpen = true
      this.$nextTick(() => {
        this.$refs.dealTagsInfoList.loadTable(row.id)
      })
    }
  }
}
</script>
